﻿
//***********************************************************************
// * Module:   MsjJdYzdhxpgmsqHandler
// * 作者:  曹荣芝
// * Modified: 2013/5/29
// * Purpose: Definition of the Class MsjJdYzdhxpgmsqHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// MsjJdYzdhxpgmsqHandler 的摘要说明
    /// </summary>
    public class MsjJdYzdhxpgmsqHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                MsjJdYzdhxpgmsq mMsjJdYzdhxpgmsq = new MsjJdYzdhxpgmsq();
                mMsjJdYzdhxpgmsq.Sqid = Guid.NewGuid().ToString(); //主键
                mMsjJdYzdhxpgmsq.Xm = context.Request.Form["texXm"]; //姓名
                mMsjJdYzdhxpgmsq.Zz = context.Request.Form["texZz"]; //住址
                mMsjJdYzdhxpgmsq.Fddbr = context.Request.Form["texFddbr"]; //法定代表人
                mMsjJdYzdhxpgmsq.Frdh = context.Request.Form["texFrdh"]; //法人电话
                mMsjJdYzdhxpgmsq.Jbr = context.Request.Form["texJbr"]; //经办人
                mMsjJdYzdhxpgmsq.Jbrdh = context.Request.Form["texJbrdh"]; //经办人电话
                mMsjJdYzdhxpgmsq.Xsdwmc = context.Request.Form["texXsdwmc"]; //销售单位名称
                mMsjJdYzdhxpgmsq.Xsdwzs = context.Request.Form["texXsdwzs"]; //销售单位住所
                mMsjJdYzdhxpgmsq.Xsfddbr = context.Request.Form["texXsfddbr"]; //销售法定代表人
                mMsjJdYzdhxpgmsq.Xsfddbrdh = context.Request.Form["texXsfddbrdh"]; //销售法定代表人电话
                mMsjJdYzdhxpgmsq.Pz = context.Request.Form["texPz"]; //品种
                mMsjJdYzdhxpgmsq.Sl = context.Request.Form["texSl"]; //数量
                mMsjJdYzdhxpgmsq.Yt = context.Request.Form["texYt"]; //用途
                mMsjJdYzdhxpgmsq.Nxql = context.Request.Form["texNxql"]; //年需求量
                mMsjJdYzdhxpgmsq.Scyj = context.Request.Form["texScyj"]; //审查意见
                //mMsjJdYzdhxpgmsq.Scrqm = context.Request.Form["texScrqm"]; //审查人签名
                mMsjJdYzdhxpgmsq.Scrq = context.Request.Form["texScrq"]; //审查日期
                mMsjJdYzdhxpgmsq.Spyj = context.Request.Form["texSpyj"]; //审批意见
                //mMsjJdYzdhxpgmsq.Sprqm = context.Request.Form["texSprqm"]; //审批人签名
                mMsjJdYzdhxpgmsq.Sprq = context.Request.Form["texSprq"]; //审批日期
                //mMsjJdYzdhxpgmsq.Sqrqm = context.Request.Form["texSqrqm"]; //申请人签名
                mMsjJdYzdhxpgmsq.Sqrq = context.Request.Form["texSqrq"]; //申请日期
                mMsjJdYzdhxpgmsq.Sljg = context.Request.Form["texSljg"]; //受理机关(字典)
                mMsjJdYzdhxpgmsq.Bz = context.Request.Form["texBz"]; //备注
                mMsjJdYzdhxpgmsq.Cjsj = DateTime.Now.ToString(); //创建时间(2013-05-28 09:13:01)
                mMsjJdYzdhxpgmsq.Cjr = mUser.UserId; //创建人(人员ID)
                mMsjJdYzdhxpgmsq.Cjrdw = mUser.OrgnaId; //创建单位(单位ID)
                //mMsjJdYzdhxpgmsq.Xgr = context.Request.Form["texXgr"]; //修改人(人员ID)
                //mMsjJdYzdhxpgmsq.Xgrdw = context.Request.Form["texXgrdw"]; //修改人单位(单位ID)
                //mMsjJdYzdhxpgmsq.Xgsj = context.Request.Form["texXgsj"]; //修改时间(2013-05-28 09:13:01)
                mMsjJdYzdhxpgmsq.Enabled = "1"; //是否有效（1有效，0无效）
                mMsjJdYzdhxpgmsq.Movesign = "0"; //是否传输（1已，0未）

                mResultModel = MsjJdYzdhxpgmsqBLL.Insert(mMsjJdYzdhxpgmsq, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                MsjJdYzdhxpgmsq mMsjJdYzdhxpgmsq = new MsjJdYzdhxpgmsq();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = MsjJdYzdhxpgmsqBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion
        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            MsjJdYzdhxpgmsq mMsjJdYzdhxpgmsq = new MsjJdYzdhxpgmsq();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mMsjJdYzdhxpgmsq = MsjJdYzdhxpgmsqBLL.Load(paramKey); //根据键值获取实体信息
                if (mMsjJdYzdhxpgmsq != null)
                {
                    var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                    mMsjJdYzdhxpgmsq.Xm = context.Request.Form["texXm"]; //姓名
                    mMsjJdYzdhxpgmsq.Zz = context.Request.Form["texZz"]; //住址
                    mMsjJdYzdhxpgmsq.Fddbr = context.Request.Form["texFddbr"]; //法定代表人
                    mMsjJdYzdhxpgmsq.Frdh = context.Request.Form["texFrdh"]; //法人电话
                    mMsjJdYzdhxpgmsq.Jbr = context.Request.Form["texJbr"]; //经办人
                    mMsjJdYzdhxpgmsq.Jbrdh = context.Request.Form["texJbrdh"]; //经办人电话
                    mMsjJdYzdhxpgmsq.Xsdwmc = context.Request.Form["texXsdwmc"]; //销售单位名称
                    mMsjJdYzdhxpgmsq.Xsdwzs = context.Request.Form["texXsdwzs"]; //销售单位住所
                    mMsjJdYzdhxpgmsq.Xsfddbr = context.Request.Form["texXsfddbr"]; //销售法定代表人
                    mMsjJdYzdhxpgmsq.Xsfddbrdh = context.Request.Form["texXsfddbrdh"]; //销售法定代表人电话
                    mMsjJdYzdhxpgmsq.Pz = context.Request.Form["texPz"]; //品种
                    mMsjJdYzdhxpgmsq.Sl = context.Request.Form["texSl"]; //数量
                    mMsjJdYzdhxpgmsq.Yt = context.Request.Form["texYt"]; //用途
                    mMsjJdYzdhxpgmsq.Nxql = context.Request.Form["texNxql"]; //年需求量
                    mMsjJdYzdhxpgmsq.Scyj = context.Request.Form["texScyj"]; //审查意见
                    //mMsjJdYzdhxpgmsq.Scrqm = context.Request.Form["texScrqm"]; //审查人签名
                    mMsjJdYzdhxpgmsq.Scrq = context.Request.Form["texScrq"]; //审查日期
                    mMsjJdYzdhxpgmsq.Spyj = context.Request.Form["texSpyj"]; //审批意见
                    //mMsjJdYzdhxpgmsq.Sprqm = context.Request.Form["texSprqm"]; //审批人签名
                    mMsjJdYzdhxpgmsq.Sprq = context.Request.Form["texSprq"]; //审批日期
                    //mMsjJdYzdhxpgmsq.Sqrqm = context.Request.Form["texSqrqm"]; //申请人签名
                    mMsjJdYzdhxpgmsq.Sqrq = context.Request.Form["texSqrq"]; //申请日期
                    mMsjJdYzdhxpgmsq.Sljg = context.Request.Form["texSljg"]; //受理机关(字典)
                    mMsjJdYzdhxpgmsq.Bz = context.Request.Form["texBz"]; //备注
                    //mMsjJdYzdhxpgmsq.Cjsj = context.Request.Form["texCjsj"]; //创建时间(2013-05-28 09:13:01)
                    //mMsjJdYzdhxpgmsq.Cjr = context.Request.Form["texCjr"]; //创建人(人员ID)
                    //mMsjJdYzdhxpgmsq.Cjrdw = context.Request.Form["texCjrdw"]; //创建单位(单位ID)
                    mMsjJdYzdhxpgmsq.Xgr = mUser.UserId; //修改人(人员ID)
                    mMsjJdYzdhxpgmsq.Xgrdw = mUser.OrgnaId; //修改人单位(单位ID)
                    mMsjJdYzdhxpgmsq.Xgsj = DateTime.Now.ToString(); //修改时间(2013-05-28 09:13:01)
                    mMsjJdYzdhxpgmsq.Enabled = "1"; //是否有效（1有效，0无效）
                    //mMsjJdYzdhxpgmsq.Movesign = context.Request.Form["texMovesign"]; //是否传输（1已，0未）

                    mResultModel = MsjJdYzdhxpgmsqBLL.Update(mMsjJdYzdhxpgmsq, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                MsjJdYzdhxpgmsq mMsjJdYzdhxpgmsq = new MsjJdYzdhxpgmsq(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mMsjJdYzdhxpgmsq = MsjJdYzdhxpgmsqBLL.Load(paramKey); //根据键值获取管理员
                    if (mMsjJdYzdhxpgmsq != null)
                    {
                        json = JsonConvert.SerializeObject(mMsjJdYzdhxpgmsq);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtMsjJdYzdhxpgmsq = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            string name = context.Request.Form["texName"];
            string xsdwmc = context.Request.Form["texxs"];
            string pz = context.Request.Form["texpz"];
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();
            SqlConditionList sqllist = new SqlConditionList();
            sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, MsjJdYzdhxpgmsqConst.Columns.Enabled, "1"), SqlLogicOper.AND_LOGIC);
            if (!string.IsNullOrEmpty(name))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, MsjJdYzdhxpgmsqConst.Columns.Xm, name), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(xsdwmc))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjJdYzdhxpgmsqConst.Columns.Xsdwmc, '%' + xsdwmc + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(pz))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjJdYzdhxpgmsqConst.Columns.Pz, '%' + pz + '%'), SqlLogicOper.AND_LOGIC);
            }
            dtMsjJdYzdhxpgmsq = Q.GetRecordByPage("*", sqllist.ToSql(), "SQRQ", intPageSize, intPageIndex, MsjJdYzdhxpgmsqConst.TableName, MsjJdYzdhxpgmsqConst.FieldKey, "DESC").Tables[0];
            if (dtMsjJdYzdhxpgmsq != null && dtMsjJdYzdhxpgmsq.Rows.Count > 0)
            {
                for (int i = 0; i < dtMsjJdYzdhxpgmsq.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtMsjJdYzdhxpgmsq.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtMsjJdYzdhxpgmsq;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }
        #endregion

    }
}


